<template>

      <div class="row">
        <div class="col-md-12">
          <h1 class="page-header">
            开课 <small></small>
          </h1>
        </div>
      </div>
      <!-- /. ROW  -->
      <div class="row">
        <div class="col-lg-12">
          <div class="panel panel-default">
            <div class="panel-heading">
              课程基本信息
            </div>
            <div class="panel-body">
              <div class="row grid justify-items-stretch">
                <div class="col-lg-8 justify-self-center">
                  <div class="form-group">
                    <label>课程年度</label>
                    <el-select filterable
                               allow-create v-model="classYear" class="m-2" placeholder="Select" size="large">
                      <el-option
                          v-for="item in classYearList"
                          :key="item.value"
                          :label="item.label"
                          :value="item.value"
                      >
                      </el-option>
                    </el-select>
                  </div>
                  <div class="form-group">
                    <label>课程附件</label>
                    <DataSpace @putList="putDataSpaceFileListInFileList"></DataSpace>
                    <el-upload
                        ref="upload"
                        class="upload-demo"
                        action="/api/teacher/PutFile"
                        :on-remove="handleChange"
                        :headers="fileUpHead"
                        :on-change="handleChange"
                        :file-list="fileList"
                    >
                      <el-button size="small" type="primary">选择本地文件</el-button>
                      <template #tip>
                        <div class="el-upload__tip">
                        </div>
                      </template>
                    </el-upload>
                  </div>
                  <div class="form-group">
                    <el-button size="large"  @click="putNewcurriculum">创建</el-button>
                  </div>
                </div>
                <!-- /.col-lg-6 (nested) -->
              </div>
              <!-- /.row (nested) -->
            </div>
            <!-- /.panel-body -->
          </div>
          <!-- /.panel -->
        </div>
        <!-- /.col-lg-12 -->
      </div>
</template>

<script>
import axios from "axios";
import DataSpace from "../components/DataSpace";
import moment from 'moment'
import {ElMessage} from "element-plus";

export default {
  name: "add_curriculum",
  components: {DataSpace},
  created() {

    var parse = JSON.parse(window.localStorage.getItem('zjxClass_teacherinfo'));
    this.fileUpHead = {
      'tk': parse.Token
    }

    this.setTime()

  },
  mounted() {
    this.curriculumSeriesId = this.$route.query.curriculumSeriesId
    var $findElement = $($('.upload-demo').find('input')[0]);
    $findElement.hide()
  },
  data() {
    return {
      classYearList: [],
      curriculumSeriesId: '',
      classYear: '',
      fileUpHead: {
        'dwa': "wa"
      },
      fileList: [],
    }
  },
  methods: {
    setTime() {
      var now = moment()
      for (let i = now.year() - 20; i < now.year() + 20; i++) {
        this.classYearList.push({
          value: i,
          label: i,
        },)
      }
      this.classYear=now.year()
    },
    handleChange(file, fileList) {
      this.fileList = fileList
    },
    jumpTo: function (name, data) {
      this.$router.push({name: name, query: data})
    },
    putDataSpaceFileListInFileList(list) {
      this.fileList = [...this.fileList, ...list]
    },
    putNewcurriculum() {
      console.log(this.classYear)
      console.log(this.curriculumSeriesId)
      var data = {}
      data.CurriculumYear = this.classYear+""
      data.CurriculumSeriesId = this.curriculumSeriesId
      data.CurriculumData = ""
      for (const argument of this.fileList) {
        console.log(argument.ID)
        if (argument.ID === undefined) {
          data.CurriculumData += "," + argument.response.ID
        } else {
          data.CurriculumData += "," + argument.ID
        }

      }
      console.log(data)
      axios.post("/api/teacher/PutCurriculum", data).then((res) => {
        if (res.status === 200) {
          if (res.data==='出现重复'){
            ElMessage.warning({
              message:'年份不可以重复'
            })
          }else {
            ElMessage.success({
              message:'创建成功'
            })
            this.$jump.mtJumpNq('curriculum_series_info',{'seriesId':this.curriculumSeriesId})
          }

        }
      })
    }
  }
}
</script>

<style scoped>

</style>